<?php

//if (isset($_POST['data'])) {
//
//// Instance de la class DomDocument
//    $doc = new DOMDocument();
//
//// Definition de la version et l'encodage
//    $doc->version = '1.0';
//    $doc->encoding = 'UTF-8';
//
//// Definition du format kml
////    $kml_elt = $doc->createElement('kml');
////    $kml_elt->setAttribute('xmlns', "http://www.opengis.net/kml/2.2");
////    $doc->appendChild($kml_elt);
////
////// Ajout la balise 'document' a la racine
////    $document_elt = $doc->createElement('Document');
////    $kml_elt->appendChild($document_elt);
////
////    $name_elt = $doc->createElement('name', 'MyDocument');
////    $description_elt = $doc->createElement('description', 'content');
////    $document_elt->appendChild($name_elt);
////    $document_elt->appendChild($description_elt);
////
////    $style_elt = $doc->createElement('Style');
////    $style_elt->setAttribute('id', "Lump");
////    $linestyle_elt = $doc->createElement('LineStyle');
////    $color_elt = $doc->createElement('color', 'CD0000FF');
////    $width_elt = $doc->createElement('width', '2');
////
////    $polytyle_elt = $doc->createElement('PolyStyle');
////    $colorPoly_elt = $doc->createElement('color', '9AFF0000');
////
////    $style_elt->appendChild($linestyle_elt);
////    $linestyle_elt->appendChild($color_elt);
////    $linestyle_elt->appendChild($width_elt);
////    $style_elt->appendChild($polytyle_elt);
////    $polytyle_elt->appendChild($colorPoly_elt);
////
////    $document_elt->appendChild($style_elt);
////
////    $style_elt = $doc->createElement('Style');
////    $style_elt->setAttribute('id', "Path");
////    $linestyle_elt = $doc->createElement('LineStyle');
////    $color_elt = $doc->createElement('color', 'FF0000FF');
////    $width_elt = $doc->createElement('width', '3');
////
////    $style_elt->appendChild($linestyle_elt);
////    $linestyle_elt->appendChild($color_elt);
////    $linestyle_elt->appendChild($width_elt);
////
////    $document_elt->appendChild($style_elt);
//
//    $res = json_decode($_POST['data'], true);
//    $i=0;
//    $coordinate_elt = $doc->createElement('coordinate');
//    foreach ($res as $polyline) {
//        //$mesage = '';
//        $coordinates_elt = $doc->createElement('coordinates');
//        $coordinates_elt->setAttribute('id',$i);
//        foreach ($polyline as $coordonee) {
//            //$mesage = $mesage . $coordonee[1] . '" lat="' . $coordonee[0] .'" alt="0" />' ."\n";
//            $coord_elt = $doc->createElement("coord");
//            $coord_elt->setAttribute('lng',$coordonee[1]);
//            $coord_elt->setAttribute('lat',$coordonee[0]);
//            $coord_elt->setAttribute('alt','0');
//            $coordinates_elt->appendChild($coord_elt);
//        }
////        $placemark_elt = $doc->createElement("Placemark");
////        
////        $name_elt = $doc->createElement('name', 'Name');
////        $description_elt = $doc->createElement('description', 'YES');
////        $styleURL_elt = $doc->createElement('styleURL', '#Path');
////        $placemark_elt->appendChild($name_elt);
////        $placemark_elt->appendChild($description_elt);
////        $placemark_elt->appendChild($styleURL_elt);
////        
////        $linestring_elt = $doc->createElement('LineString');
////        $altitudeMode_elt = $doc->createElement('altitudeMode', 'relative');
//        
//
////        $linestring_elt->appendChild($altitudeMode_elt);
////        $linestring_elt->appendChild($coordinates_elt);
////        
////        $placemark_elt->appendChild($linestring_elt);
//        $coordinate_elt->appendChild($coordinates_elt);
//        $i++;
//    }
//$doc->appendChild($coordinate_elt);
////    $linestring_elt = $doc->createElement('LineString');
////    $tessellate_elt = $doc->createElement('tessellate', '1');
////    $altitudeMode_elt = $doc->createElement('altitudeMode', 'clampToGround');
////    $coordinates_elt = $doc->createElement('coordinates');
//
//
//
//    $doc->formatOutput = true;
//
//// Afficher le document XML
//    //echo $doc->saveXML();
//    echo $mesage;
////Sauver le document XML sous le nom simple.xml
//    $doc->save('./parcours/simple3.xml');
//} else {
//    echo "erreur";
//}
// on essaie de reconnaitre l'extension pour que le téléchargement corresponde au type de fichier afin d'éviter les erreurs de corruptions 

if (isset($_GET['Fichier_a_telecharger'])) {
    
    $Fichier_a_telecharger = ($_GET['Fichier_a_telecharger']);
    
    switch (strrchr(basename($Fichier_a_telecharger), ".")) {

        case ".gz": $type = "application/x-gzip";
            break;
        case ".tgz": $type = "application/x-gzip";
            break;
        case ".zip": $type = "application/zip";
            break;
        case ".pdf": $type = "application/pdf";
            break;
        case ".png": $type = "image/png";
            break;
        case ".gif": $type = "image/gif";
            break;
        case ".jpg": $type = "image/jpeg";
            break;
        case ".txt": $type = "text/plain";
            break;
        case ".htm": $type = "text/html";
            break;
        case ".html": $type = "text/html";
            break;
        default: $type = "application/octet-stream";
            break;
    }

    header("Content-disposition: attachment; filename=$Fichier_a_telecharger");
    header("Content-Type: application/force-download");
    header("Content-Transfer-Encoding: $type\n"); // Surtout ne pas enlever le \n
    header("Content-Length: " . filesize($Fichier_a_telecharger));
    header("Pragma: no-cache");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0, public");
    header("Expires: 0");
    readfile($Fichier_a_telecharger);
}
?>
